<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Iris
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

abstract class Iris_Abstract_Controller
{
	protected $template = null;
	protected $output  = array();
	protected $session = array();
	protected $breadcrumbs = array();
	protected $pageTitle = null;

	protected $config = array();

	public function __construct()
	{
		$configHandler = new Iris_Model_ConfigHandler;
		$this->config  = $configHandler->getConfigs();

		$this->output['config'] =& $this->config;
		$this->output['url']     = Iris::$url;
		$this->output['dirname'] = Iris::$dirname;

		$this->output['controller'] = Iris::$_controller;
		$this->output['action']     = Iris::$_action;

		if ( !isset($_SESSION[Iris::$dirname]) )
		{
			$_SESSION[Iris::$dirname] = array();
		}

		$this->session =& $_SESSION[Iris::$dirname];

		$this->breadcrumbs[] = array(
			'name' => Iris::$cms->moduleName,
			'url'  => Iris::$url.'/',
		);

		$this->output['breadcrumbs'] =& $this->breadcrumbs;
		$this->output['page_title']  =& $this->pageTitle;
	}

	public function main()
	{
	}

	protected function _view()
	{
		if ( !$this->template )
		{
			$this->template = 'db:'.Iris::$dirname.'.'.Iris::$_controller.'.'.Iris::$_action.'.tpl';
		}

		$smarty = Iris::$cms->getSmarty();
		$this->_escapeHtml($this->output);
		$smarty->assign('iris', $this->output);
		$smarty->register_modifier('t', 'Iris::t');
		$smarty->register_modifier('raw', 'Iris_Class_Smarty::raw');
		$smarty->register_function('url', 'Iris_Class_Smarty::url');
		$smarty->register_modifier('currency', 'Iris_Class_Smarty::currency');
		$smarty->display($this->template);

		$this->_head($smarty);
		$this->_pageTitle();

		global $xoopsTpl;
		$xoopsTpl->assign('xoops_breadcrumbs', $this->output['breadcrumbs']);
	}

	protected function _escapeHtml(&$vars)
	{
		foreach ( $vars as $key => &$var )
		{
			if ( is_array($var) )
			{
				$this->_escapeHtml($var);
			}
			elseif ( !is_object($var) )
			{
				$var = Iris::escapeHtml($var);
			}
		}
	}

	protected function _head($smarty)
	{
		Iris::$cms->addHeader('<link rel="stylesheet" type="text/css" media="screen" href="'.Iris::$url.'/public/css/stylesheet.css" />');

		if ( file_exists(IRIS_PATH.'/public/css/'.Iris::$_controller.'.css') )
		{
			Iris::$cms->addHeader('<link rel="stylesheet" type="text/css" media="screen" href="'.Iris::$url.'/public/css/'.Iris::$_controller.'.css" />');
		}

		if ( class_exists('Iris_Jquery', false) ) return;
		return;
		$head = $smarty->fetch('db:'.Iris::$dirname.'.head.tpl');
		Iris::$cms->addHeader($head);
	}

	protected function _pageTitle()
	{
		if ( !$this->pageTitle )
		{
			return;
		}

		$this->breadcrumbs[] = array(
			'url'  => null,
			'name' => $this->pageTitle,
		);

		global $xoopsTpl;
		$xoopsTpl->assign('xoops_pagetitle', $this->pageTitle);
	}
}

?>
